unit VarGrabber;

interface

uses
  Database, Vars,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Db, DBTables;

type
  TFormVarGrab = class(TForm)
    BtnOK1: TBitBtn;
    BtnCancel: TBitBtn;
    ListUser: TListBox;
    ListSystem: TListBox;
    Label2: TLabel;
    Label1: TLabel;
    ListEvaluators: TListBox;
    ListReference: TListBox;
    Label5: TLabel;
    Label4: TLabel;
    Label4a: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label5a: TLabel;
    BtnOK2: TBitBtn;
    BtnOK3: TBitBtn;
    BtnOK4: TBitBtn;
    BtnVars: TBitBtn;
    a1: TImage;
    a2: TImage;
    a3: TImage;
    a6: TImage;
    a5: TImage;
    Bevel1: TBevel;
    LabelValue: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure BtnOK1Click(Sender: TObject);
    procedure ListUserKeyPress(Sender: TObject; var Key: Char);
    procedure BtnOK2Click(Sender: TObject);
    procedure BtnOK3Click(Sender: TObject);
    procedure BtnOK4Click(Sender: TObject);
    procedure BtnVarsClick(Sender: TObject);
    procedure ListUserDrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure ListUserClick(Sender: TObject);
  private
    { Private declarations }
    procedure RefreshListUser;
  public
    { Public declarations }
    grab: string;
    procedure Setup;
  end;

var
  FormVarGrab: TFormVarGrab;

implementation
uses
   DBClient;
{$R *.DFM}

procedure TFormVarGrab.FormCreate(Sender: TObject);
begin
  grab:='';
  a1.left:=16;
  a1.top:=378;
  a2.left:=a1.left;
  a2.top:=a1.top;
  a3.left:=a1.left;
  a3.top:=a1.top;
  a5.left:=a1.left;
  a5.top:=a1.top;
  a6.left:=a1.left;
  a6.top:=a1.top;
end;

procedure TFormVarGrab.Setup;
var i: integer;
begin
  RefreshListUser;
  //System variables
  ListSystem.items.clear;
  for i:=0 to Datawell.rvars.count-1 do ListSystem.items.add(Datawell.rvars[i]);
  ListSystem.itemindex:=1;
  ListSystem.itemindex:=0;
  //System evaluators
  ListEvaluators.items.add('IMAGE');
  ListEvaluators.items.add('AVI');
  ListEvaluators.items.add('SOUND');
  ListEvaluators.items.add('MUSIC');
  ListEvaluators.items.add('MWCOMBAT');
  ListEvaluators.items.add('MWGUILD');
  ListEvaluators.items.add('AUTOTYPER');
  ListEvaluators.items.add('SYSTEM');
  ListEvaluators.items.add('EXECUTE');
  ListEvaluators.itemindex:=1;
  ListEvaluators.itemindex:=0;
  //System references
  ListReference.items.add('TIME');
  ListReference.items.add('DATE');
  ListReference.items.add('CHARTING');
  ListReference.items.add('FOLLOWING');
  ListReference.items.add('ROOM');
  ListReference.itemindex:=1;
  ListReference.itemindex:=0;
end;

procedure TFormVarGrab.BtnCancelClick(Sender: TObject);
begin
  close;
end;

procedure TFormVarGrab.ListUserKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    case TComponent(sender).tag of
      1: BtnOK1.click;
      2: BtnOK2.click;
      3: BtnOK3.click;
      4: BtnOK4.click;
    end;
  end;
end;

procedure TFormVarGrab.BtnOK1Click(Sender: TObject);
var i: integer;
begin
  i:=ListUser.itemindex;
  if i < 0 then exit;
  grab:='@'+ListUser.items[i];
  close;
end;

procedure TFormVarGrab.BtnOK2Click(Sender: TObject);
var i: integer;
begin
  i:=ListSystem.itemindex;
  if i < 0 then exit;
  grab:='@'+ListSystem.items[i];
  close;
end;

procedure TFormVarGrab.BtnOK3Click(Sender: TObject);
var i: integer;
begin
  i:=ListEvaluators.itemindex;
  if i < 0 then exit;
  grab:='@'+ListEvaluators.items[i];
  close;
end;

procedure TFormVarGrab.BtnOK4Click(Sender: TObject);
var i: integer;
begin
  i:=ListReference.itemindex;
  if i < 0 then exit;
  grab:='@'+ListReference.items[i];
  close;
end;

procedure TFormVarGrab.BtnVarsClick(Sender: TObject);
var form: TFormVars;
begin
  form:=TFormVars.create(self);
  form.nodelete:=true;
  form.BtnDelete.enabled:=false;
  form.showmodal;
  form.free;
  RefreshListUser;
end;

procedure TFormVarGrab.RefreshListUser;
var table: TClientDataset;
begin
  //User variables
  ListUser.items.clear;
  table:=TClientDataset.create(self);
  with table do begin
    clonecursor(Datawell.TableVariables, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      ListUser.items.add(fieldbyname('FVariable').asstring);
      next;
    end;
  end;
  table.free;
  if ListUser.items.count < 1 then BtnOK1.enabled:=false
  else begin
    BtnOK1.enabled:=true;
    ListUser.itemindex:=1;
    ListUser.itemindex:=0;
  end;
end;

procedure TFormVarGrab.ListUserDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  bitmap: TBitmap;
  str: string;
begin
  with TListBox(control).canvas do begin
    FillRect(Rect);
    case TListBox(control).tag of
      1: bitmap:=a1.picture.bitmap;
      2: begin
           str:=TListBox(control).items[index];
           if str = 'HP' then bitmap:=a2.picture.bitmap
           else if str = 'MAXHP' then bitmap:=a2.picture.bitmap
           else if str = 'SP' then bitmap:=a2.picture.bitmap
           else if str = 'MAXSP' then bitmap:=a2.picture.bitmap
           else if str = 'GP1' then bitmap:=a2.picture.bitmap
           else if str = 'MAXGP1' then bitmap:=a2.picture.bitmap
           else if str = 'GP2' then bitmap:=a2.picture.bitmap
           else if str = 'MAXGP2' then bitmap:=a2.picture.bitmap
           else if str = 'ENEMY' then bitmap:=a2.picture.bitmap
           else if str = 'ENAME' then bitmap:=a2.picture.bitmap
           else if str = 'GLINE1' then bitmap:=a2.picture.bitmap
           else if str = 'GLINE2' then bitmap:=a2.picture.bitmap
           else if str = 'CAPTION' then bitmap:=a2.picture.bitmap
           else bitmap:=a3.picture.bitmap
         end;
      3: bitmap:=a5.picture.bitmap;
      4: bitmap:=a6.picture.bitmap;
      else bitmap := nil;
    end;
    if Bitmap <> nil then begin
      BrushCopy(Bounds(Rect.left+2,Rect.top,bitmap.width,bitmap.height),bitmap,
        Bounds(0,0,Bitmap.width,Bitmap.height),clsilver);
    end;
    TextOut(Rect.left+16,Rect.top,TListBox(control).items[Index])
  end;
end;

procedure TFormVarGrab.ListUserClick(Sender: TObject);
var value: string;
begin
  a1.hide;
  a2.hide;
  a3.hide;
  a5.hide;
  a6.hide;
  if TComponent(sender).tag = 3 then begin
    LabelValue.caption:='Click on any reference variable to see the current value';
    exit;
  end;
  value:=TListBox(sender).items[TListBox(sender).itemindex];
  case TComponent(sender).tag of
    1: a1.show;
    2: begin
         if (value = 'AA') or (value = 'BB') or (value = 'CC') or
           (value = 'DD') or (value = 'EE') or (value = 'FF') then a3.show else a2.show;
       end;
    4: a6.show;
  end;
  LabelValue.caption:=value+' = ';
  value:=Datawell.ReplaceVariables('@'+value);
  if value = '' then value:='<empty>';
  LabelValue.caption:=LabelValue.caption+value;
end;

end.
